<?php

namespace Xsjosg\Zt\App\Webapp\Sixyear251014\Cpanel\Traits;

use Exception;
use Xsjosg\Zt\App\Webapp\Sixyear251014\Models\RewardLog;
use Xsjosg\Zt\Core\Helper\Misc;
use Xsjosg\Zt\Core\Helper\Str;

trait RewardLogControllerTrait
{
    public function rewardLogListAction()
    {
        $options['query'] = RewardLog::slave()->orderBy('id', 'desc');

        $data['id'] = trim(app('request')->input('id'));
        $data['user_id'] = trim(app('request')->input('user_id'));
        $data['server_id'] = trim(app('request')->input('server_id'));
        $data['reward_type'] = trim(app('request')->input('reward_type'));
        $data['status'] = trim(app('request')->input('status'));
        $data['create_time'] = trim(app('request')->input('create_time'));
        $data['update_time'] = trim(app('request')->input('update_time'));

        if (strlen($data['id'])) {
            $options['query']->where('id', $data['id']);
        }
        if (strlen($data['user_id'])) {
            $options['query']->where('user_id', $data['user_id']);
        }
        if (strlen($data['server_id'])) {
            $options['query']->where('server_id', $data['server_id']);
        }
        if (strlen($data['reward_type'])) {
            $options['query']->where('reward_type', $data['reward_type']);
        }
        if (strlen($data['status'])) {
            $options['query']->where('status', $data['status']);
        }
        if (strlen($data['create_time'])) {
            list($beginTime, $endTime) = Str::explodeRangeTime($data['create_time'], true);
            $options['query']->whereBetween('create_time', [$beginTime, $endTime]);
        }
        if (strlen($data['update_time'])) {
            $options['query']->where('update_time', $data['update_time']);
        }

        $options['title'] = '领奖记录';
        $options['listActions'] = [
            [
                'desc' => '全部',
                'icon' => 'fire',
                'link' => '@reward_log_list',
                'target' => '_self',
            ],
            [
                'desc' => '查找',
                'icon' => 'search',
                'link' => '@reward_log_search',
            ],
            [
                'desc' => '导出',
                'icon' => 'export',
                'link' => '@reward_log_export',
                'data' => app('request')->query(),
                'target' => '_self',
            ],
        ];
        $options['fields'] = [
            ['desc' => 'ID', 'name' => 'id'],
            ['desc' => '角色id', 'name' => 'user_id'],
            ['desc' => '服务器id', 'name' => 'server_id'],
            ['desc' => '奖励类型', 'name' => 'reward_type'],
            ['desc' => '奖励状态', 'name' => 'status'],
            ['desc' => '创建时间', 'name' => 'create_time'],
            ['desc' => '更新时间', 'name' => 'update_time'],
        ];
        $options['formatter'] = function ($row) {
            $row = array_map('htmlspecialchars', $row);
            $row['reward_type'] = RewardLog::$rewardTypeMap[$row['reward_type']];
            $row['status'] = RewardLog::formatStatus($row['status']);
            $row['create_time'] = date('Y-m-d H:i:s', $row['create_time']);
            $row['update_time'] = date('Y-m-d H:i:s', $row['update_time']);
            return $row;
        };

        return app('cpanel.view')->createListPage($options);
    }

    public function rewardLogSearchAction()
    {
        $options['title'] = '查找';
        $options['action'] = '@reward_log_list';
        $options['submit'] = ['form' => true];
        $options['fields'] = [
            ['desc' => 'ID', 'name' => 'id'],
            ['desc' => '角色id', 'name' => 'user_id'],
            ['desc' => '服务器id', 'name' => 'server_id'],
            ['desc' => '奖励类型', 'name' => 'reward_type', 'type' => 'select', 'options' => ['' => '全部'] + RewardLog::$rewardTypeMap],
            ['desc' => '奖励状态', 'name' => 'status', 'type' => 'select', 'options' => ['' => '全部'] + RewardLog::$statusMap],
        ];
        return app('cpanel.view')->createModalForm($options);
    }

    public function rewardLogExportAction()
    {
        $query = RewardLog::slave()->orderBy('id', 'asc');

        $data['id'] = trim(app('request')->input('id'));
        $data['user_id'] = trim(app('request')->input('user_id'));
        $data['server_id'] = trim(app('request')->input('server_id'));
        $data['reward_type'] = trim(app('request')->input('reward_type'));
        $data['status'] = trim(app('request')->input('status'));
        $data['create_time'] = trim(app('request')->input('create_time'));
        $data['update_time'] = trim(app('request')->input('update_time'));

        if (strlen($data['id'])) {
            $query->where('id', $data['id']);
        }
        if (strlen($data['user_id'])) {
            $query->where('user_id', $data['user_id']);
        }
        if (strlen($data['server_id'])) {
            $query->where('server_id', $data['server_id']);
        }
        if (strlen($data['reward_type'])) {
            $query->where('reward_type', $data['reward_type']);
        }
        if (strlen($data['status'])) {
            $query->where('status', $data['status']);
        }
        if (strlen($data['create_time'])) {
            list($beginTime, $endTime) = Str::explodeRangeTime($data['create_time'], true);
            $query->whereBetween('create_time', [$beginTime, $endTime]);
        }
        if (strlen($data['update_time'])) {
            $query->where('update_time', $data['update_time']);
        }

        $data['query'] = $query;
        $data['formatter'] = function ($row) {
            $row['reward_type'] = RewardLog::$rewardTypeMap[$row['reward_type']];
            $row['status'] = RewardLog::$statusMap[$row['status']];
            $row['create_time'] = date('Y-m-d H:i:s', $row['create_time']);
            $row['update_time'] = date('Y-m-d H:i:s', $row['update_time']);
            return [
                'ID' => $row['id'],
                '角色id' => $row['user_id'],
                '服务器id' => $row['server_id'],
                '奖励类型' => $row['reward_type'],
                '奖励状态' => $row['status'],
                '创建时间' => $row['create_time'],
                '更新时间' => $row['update_time'],
            ];
        };

        return Misc::exportCsv($data, null, 'reward_log_'.date('Ymd_His').'.csv');
    }
}
